1512C - A-B Palindrome - CodeForces Solution


constructive algorithms implementation strings *1200

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    a, b = list(map(int, input().split()))
    s = list(input())
    n = len(s)

    if ( a%2==1 and b%2==1 ) or ((a%2==1 or b%2==1)and (a+b)%2==0):
        print(-1)
        continue
    
    stop=0
    for i in range(n):
        if s[i] == '?':
            continue
        if s[i] == '1':
            if s[n-1-i] == '0':
                stop=1
                break
        if s[i] == '0':
            if s[n-1-i] == '1':
                stop=1
                break
        
        s[n-i-1] = s[i]

    if stop:
        print(-1)
        continue

    count_a = count_b = 0
    for c in s:
        if c=='1':
            count_b+=1
        elif c=='0':
            count_a+=1
    
    if s[n//2 if n%2==1 else n//2-1]=='?':
        if a%2==1:
            s[n//2 if n%2==1 else n//2-1] = '0'
            count_a +=1
        elif b%2==1:
            s[n//2 if n%2==1 else n//2-1] = '1'
            count_b +=1

    i=0
    j=n-1
    while j>=i:
        if s[i] == '?':
            if count_a < a:
                s[i] = s[n-1-i] = '0'
                count_a += (1 if n-1-i==i else 2)
            elif count_b < b:
                s[i] = s[n-1-i] = '1'
                count_b += (1 if n-1-i==i else 2)

        i+=1
        j-=1

    print(-1 if count_a!=a or count_b!=b else ''.join(s))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{   

    int t;
    cin>>t;
    
    while(t--){
        
        int a,b;
        cin>>a>>b;
        
        string s;
        cin>>s;
        int n =s.length();
        
        
        bool ans = true;
        int l = 0;
        int r = s.length()-1;
        vector<int> ind;
        
        while(l<r){
            
            if(s[l]==s[r] &&s[l]=='?') {
                ind.push_back(l);
            }
            else if(s[l]=='?'){
                if(s[r]=='1') b-=2;
                else a-=2;
                s[l]=s[r];
            }
            else if(s[r]=='?'){
                if(s[l]=='1') b-=2;
                else a-=2;
                s[r]=s[l];
            }
            else if(s[l]!=s[r]){
                ans =false;
                break;
            }
            else{
                if(s[l]=='1') b-=2;
                else a-=2;
            }
            l++;
            r--;
        }
        
        if(l==r){
            if(s[l]=='0') a--;
            else if(s[l]=='1') b--;
            else{
                ind.push_back(l);
            }
        }
        // cout<<a<<" "<<b<<"\n";
        // for(auto i : ind) cout<<i<<" ";
        // cout<<"\n";
        for(int j=0; j<ind.size();j++){
            int i = ind[j]; 
            if(i!=(n-1-i)){
                if(a>=2){
                    // cout<<"h"<<" "<<i<<"\n";
                    a-=2;
                    s[i]='0';
                    s[n-1-i]='0';
                }
                else if(b>=2){
                    b-=2;
                    s[i]='1';
                    s[n-1-i]='1';
                }
            }
            else{
                if(a>=1){
                    a-=1;
                    s[i]='0';
                }
                else if(b>=1){
                    b-=1;
                    s[i]='1';
                }
            }
        }
        
        if(ans && b==0 && a==0){
            for(int i=0; i<s.length(); i++) cout<<s[i];
            cout<<"\n";
        }
        else cout<<-1<<"\n";
        
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1722D - Line
1722C - Word Game
1722G - Even-Odd XOR
552E - Vanya and Brackets
933A - A Twisty Movement
1722F - L-shapes
1196B - Odd Sum Segments
1325D - Ehab the Xorcist
552B - Vanya and Books
1722E - Counting Rectangles
168A - Wizards and Demonstration
168B - Wizards and Minimal Spell
7A - Kalevitch and Chess
912B - New Year's Eve
1537C - Challenging Cliffs
879B - Table Tennis
1674E - Breaking the Wall
1282A - Temporarily unavailable
1366C - Palindromic Paths
336A - Vasily the Bear and Triangle
926A - 2-3-numbers
276D - Little Girl and Maximum XOR
1253C - Sweets Eating
1047A - Little C Loves 3 I
758D - Ability To Convert
733A - Grasshopper And the String
216A - Tiling with Hexagons
1351B - Square
1225A - Forgetting Things
1717A - Madoka and Strange Thoughts